package com.xwd.dao.impl;

import com.xwd.dao.DBUtils;
import com.xwd.dao.RoleDao;
import com.xwd.domain.Role;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends DBUtils implements RoleDao{
    private String sql = null;

    @Override
    public List<Role> getRoleNameList() {
        List<Role> rList = new ArrayList<>();
        try {
            sql = "select * from role where rolestate=1";
            resultSet = query(sql, null);
            while (resultSet.next()){
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                rList.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return rList;
    }

    @Override
    public List<Role> getRoleList(int pageIndex, int pageSize) {
        List<Role> rList = new ArrayList<>();
        try {
            sql = " select * from role limit ?,? ";
            List param = new ArrayList();
            param.add((pageIndex-1)*pageSize);
            param.add(pageSize);
            resultSet = query(sql, param);
            while (resultSet.next()){
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));
                rList.add(role);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return rList;
    }

    @Override
    public int getTotal() {
        int total=0;
        try {
            sql = " select count(*) from role";
            resultSet = query(sql, null);
            while (resultSet.next()){
                total=resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll();
        }
        return total;
    }

    @Override
    public int insertRole(Role role) {
        int key=0;
        try {
            sql= " insert into role values(null,?,?)";
            List param = new ArrayList();
            param.add(role.getRoleName());
            param.add(role.getRoleState());
            int i = update(sql, param);
            ResultSet generatedKeys = pps.getGeneratedKeys();//pps参数里设置了key类型，然后通过这个来返回受影响行数对应的key
            if(generatedKeys.next()){
                key = generatedKeys.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return key;
    }

    @Override
    public Role getRoleByid(int roleId) {
        Role role = new Role();
        try {
            sql=" select * from role where roleid=?";
            List param = new ArrayList<>();
            param.add(roleId);
            resultSet = query(sql, param);
            while (resultSet.next()){
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("roleName"));
                role.setRoleState(resultSet.getInt("roleState"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return role;
    }

    @Override
    public int updateRole(int roleId, String roleName, String roleState) {
        int i=0;
        try {
            sql="update role set rolename=?,rolestate=? where roleid=?";
            List param = new ArrayList();
            param.add(roleName);
            param.add(roleState);
            param.add(roleId);
            i = update(sql, param);

        } catch (Exception throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }

    @Override
    public int delete(int roleId) {
        int i=0;
        try {
            sql="delete from role where roleid=?";
            List param = new ArrayList();
            param.add(roleId);
            i = update(sql, param);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return i;
    }
}
